package games.hearthstone;

import java.util.Random;

public class HSUtils {

	private static final Random random = new Random();
	private static final int probabilityFactor;
	private static final int[] rarityProbabilitySum = new int[Constants.cardRarities.length];

	static {
		int sum = 0;
		for (int i = 0; i < Constants.rarityProbability.length; i++) {
			sum+=Constants.rarityProbability[i];
			rarityProbabilitySum[i] = sum;
		}
		probabilityFactor = sum;
	}

	public static int getRarityIndex(Rarity rarity) {
		for (int i = 0; i < Constants.cardRarities.length; i++) {
			if (rarity.equals(Constants.cardRarities[i])) {
				return i;
			}
		}
		return -1;
	}

	public static int convertToDust(Card card) {
		int index = HSUtils.getRarityIndex(card.getRarity());
		if (card.isGolden()) {
			return Constants.dustRewardGolden[index];
		} else {
			return Constants.dustRewardNormal[index];
		}
	}

	public static Card getRandomCard() {
		int cardRarity = getRandomCardRarity();
		while (Constants.rarityQuantities[cardRarity] == 0) {
			cardRarity = getRandomCardRarity();
		}

		int cardNumber = random.nextInt(Constants.rarityQuantities[cardRarity]) + 1;

		boolean golden = isGolden(cardRarity);

		return new Card(cardNumber, Constants.cardRarities[cardRarity],golden);
	}

	private static boolean isGolden(int i) {
		return random.nextFloat() < Constants.goldenProbability[i];
	}

	private static int getRandomCardRarity() {
		int rnd = random.nextInt(probabilityFactor) + 1;

		for (int i = 0; i < rarityProbabilitySum.length; i++) {
			if (i == 0 && rnd <= rarityProbabilitySum[0]) {
				return 0;
			} else if (i > 0 && rarityProbabilitySum[i-1] <= rnd && rnd <= rarityProbabilitySum[i]) {
				return i;
			}
		}

		return -1;
	}

	public static Playset getCurrentPlayset() {
		Playset playset = new Playset();

		int[] commonCards = new int[] {
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
		};
		int[] rareCards = new int[] {
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
		};
		int[] epicCards = new int[] {
				1,
				2,
				2,
				2,
				2,
				1,
				2,
				1,
				2,
				2,
				2,
				2,
				1,
				2,
				2,
				1,
				0,
				2,
				2,
				1,
				2,
				1,
				2,
				0,
				2,
				2,
				2,
				2,
				2,
				2,
				2,
				1,
				2,
				1,
				2,
				1,
				2,
		};
		int[] legendaryCards = new int[] {
				1,
				1,
				0,
				1,
				1,
				0,
				1,
				0,
				0,
				0,
				0,
				1,
				1,
				0,
				1,
				1,
				1,
				1,
				0,
				1,
				0,
				0,
				1,
				0,
				1,
				1,
				0,
				0,
				0,
				1,
				0,
				0,
				0,
		};

		addCardsToPlayset(playset, commonCards, Rarity.Common);
		addCardsToPlayset(playset, rareCards, Rarity.Rare);
		addCardsToPlayset(playset, epicCards, Rarity.Epic);
		addCardsToPlayset(playset, legendaryCards, Rarity.Legendary);

		playset.addGold(12870);

		return playset;
	}

	private static void addCardsToPlayset(Playset playset, int[] cards,
			Rarity rarity) {
		for (int i = 0; i < cards.length; i++) {
			int quantity = cards[i];
			for (int j = 0; j < quantity; j++) {
				playset.addCard(new Card(i + 1, rarity, false));
			}
		}
	}
}
